package yaq.mini.weibo.leafwb.service;

import org.springframework.stereotype.Service;
import yaq.mini.weibo.leafwb.mapper.FavouriteCollectionMapper;
import yaq.mini.weibo.leafwb.pojo.FavouriteCollection;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class FavouriteCollectionService {

    @Resource
    FavouriteCollectionMapper favouriteCollectionMapper;

    /**
     * 列出userId用户所收藏的所有文章Id
     *
     * @param userId 用户Id
     * @return
     */
    public List<String> listArticleIdByCollect(String userId) {
        List<String> articleIds = favouriteCollectionMapper.getArticleIdsByCollect(userId, "T");
        return articleIds;
    }

    /**
     * 获得userId用户点赞过的文章Id
     *
     * @param userId
     * @return
     */
    public List<String> listArticleIdByLike(String userId) {
        List<String> likerList = new ArrayList<>();
        try {
            likerList = favouriteCollectionMapper.getArticleIdsByLike(userId, "T");
        } catch (Exception e) {
            e.printStackTrace();
            return likerList;
        }
        return likerList;
    }

    /**
     * 获得点赞过该文章得用户Id
     * @param articleId 文章Id
     * @return
     */
    public List<String> listUserIdByLike(String articleId) {
        List<String> likes = new ArrayList<>();
        try {
            likes = favouriteCollectionMapper.getUserIdsByLike(articleId, "T");
        } catch (Exception e) {
            e.printStackTrace();
            return likes;
        }
        return likes;
    }

    /**
     * 获得userId用户屏蔽的文章Id
     *
     * @param userId
     * @return
     */
    public List<String> listArticleIdByShield(String userId) {
        List<String> shieldList = null;
        try {
            shieldList = favouriteCollectionMapper.getArticleIdsByShield(userId, "T");
        } catch (Exception e) {
            e.printStackTrace();
            return shieldList;
        }
        return shieldList;
    }

    public boolean updateByShield(String articleId, String userId, String isShield) {
        try {
            FavouriteCollection favouriteCollection = favouriteCollectionMapper.getFavouriteCollectionByID(userId, articleId);
            FavouriteCollection newFC = new FavouriteCollection(userId, articleId, "F", "F", isShield);
            if (favouriteCollection == null)
                favouriteCollectionMapper.insert(newFC);
            else {
                favouriteCollectionMapper.updateShield(newFC);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
}
